<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/4
     * Time: 下午2:50
     */

    namespace app\payment\home;


    use app\common\utils\UtilXml;
    use app\payment\logic\OmniPayAliPay;
    use think\Controller;
    use think\Db;
    use think\Hook;
    use think\Log;

    /**
     * 统一支付回调
     * Class Callback
     *
     * @package app\payment\api
     */
    class Callback extends Controller
    {

        public function test()
        {

            file_put_contents(Env::get('runtime_path') . 'log/pay.log',json_encode([$_POST,request()->param()]) . PHP_EOL,

                FILE_APPEND);
            return 'test';
        }

        public function wxpay()
        {

            Log::wechat(time() . '微信来了');

            //解析微信XML
            $wx_data = UtilXml::RequestXml();

            $log['pay_type']    = 'wxpay';
            $log['log_content'] = json_encode($wx_data,true);
            $log['create_time'] = time();
            Db::name('payment_log')->insert($log);
            if ($wx_data['return_code'] == 'SUCCESS' && $wx_data['result_code'] == 'SUCCESS')
            {//执行操作行为
                Hook::listen('wxpay_collback',$wx_data);
            }

            //告诉微信 信息已经收到！
            echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

            //存储日志
            Log::wechat(time() . '微信走了');
        }

        public function alipay()
        {

            Log::wechat(time() . '支付宝来了');
            file_put_contents(Env::get('runtime_path') . 'log/pay.log',json_encode([
                $_POST,
                request()->param()
            ]),FILE_APPEND);
            OmniPayAliPay::callback();

            //存储日志
            Log::wechat(time() . '支付宝走了');
        }
    }